layui.config({
	base: '/static/build/js/'
}).use(['ztree', 'layer'], function() {
	var $ = layui.$;
	var layer = layui.layer;
	var setting = {
		view: {
			showLine: true,
			showTitle: true,
			addHoverDom: addHoverDom,
			removeHoverDom: removeHoverDom,
			selectedMulti: false
		},
		edit: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			onClick: deptTreeClick,
			beforeRemove: beforeRemove,
			beforeEditName: beforeEditName
		}
	}
	loadDeptTree();
	var editNodeID = "";
	var operFlag = "view";

	function loadDeptTree() {
		$.get(
			"/static/menutree.json",
			function(result) {
				if(result.code == 0) {
					var zNodes = result.data;
					//加载tree
					$.fn.zTree.init($("#ztree"), setting, zNodes);
					//根节点被选中
					var treeObj = $.fn.zTree.getZTreeObj("ztree");
					var node = treeObj.getNodeByParam("id", 0, null);
					treeObj.selectNode(node);
					deptTreeClick(event, "ztree", node);
				}
			},
			"json"
		);
	}

	function deptTreeClick(event, treeId, treeNode) {
		$("#name").val(treeNode.name);
		$("#pId").val(treeNode.pId);
		if(treeNode.name == "全部") {
			$("#pName").val("root");
		} else {
			$("#pName").val($.fn.zTree.getZTreeObj(treeId).getNodeByParam("id", treeNode.pId, null).name || "root");
		}
		$("#ordernum").val(treeNode.ordernum);
		$("#code").val(treeNode.menucode);
		$("#address").val(treeNode.url);
		$("#layuicode").val(treeNode.icon);
		operFlag = "view";
		readonlyTrue();
	}
	var newCount = 1;

	function addHoverDom(treeId, treeNode) {
		var sObj = $("#" + treeNode.tId + "_span");
		if(treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
		var addStr = "<span class='button add' id='addBtn_" + treeNode.tId +
			"' title='增加' onfocus='this.blur();'></span>";
		sObj.after(addStr);

		var btn = $("#addBtn_" + treeNode.tId);
		if(btn) btn.bind("click", function() {
			$("#name").val("");
			$("#code").val("");
			$("#address").val("");
			if(treeNode.pId) {
				$("#parentMenucode").val(treeNode.id);
				$("#parentMenuname").val(treeNode.name);
			} else {
				$("#parentMenucode").val("0");
				$("#parentMenuname").val("root");
			}
			icon = "&#xe63c;";
			$("#icon").val("&#xe63c;");
			$("#ordernum").val(0);

			operFlag = "add";
			readonlyFalse();
			return false;
		});

	};
	//删除
	function beforeRemove(treeId, treeNode) {
		var flag = true;
		if(treeNode && treeNode.id == "0") {
			layer.msg(
				"根节点不可删除", {
					time: 1500
				},
			);
			flag = false;
		}
		return flag;
	}
	//修改
	function beforeEditName(treeId, treeNode) {
		editNodeID = treeNode.id;
		$("#name").val(treeNode.name);
		$("#pId").val(treeNode.pId);
		if(treeNode.name == "全部") {
			$("#pName").val("root");
		} else {
			$("#pName").val($.fn.zTree.getZTreeObj(treeId).getNodeByParam("id", treeNode.pId, null).name || "root");
		}
		$("#ordernum").val(treeNode.ordernum);
		$("#code").val(treeNode.menucode);
		$("#address").val(treeNode.url);
		$("#layuicode").val(treeNode.icon);
		operFlag = "edit";
		readonlyFalse();
		return false;
	}

	function removeHoverDom(treeId, treeNode) {
		$("#addBtn_" + treeNode.tId).unbind().remove();
	};
	//关闭只读
	function readonlyFalse() {
		$("#name").removeAttr("readonly");
		$("#code").removeAttr("readonly");
		$("#address").removeAttr("readonly");
		$("#layuicode").removeAttr("readonly");
		$("#ordernum").removeAttr("readonly");
	}

	//开启只读
	function readonlyTrue() {
		$("#name").attr("readonly", "readonly");
		$("#code").attr("readonly", "readonly");
		$("#address").attr("readonly", "readonly");
		$("#layuicode").attr("readonly", "readonly");
		$("#ordernum").attr("readonly", "readonly");
	}
});